﻿using System.Collections.Generic;
using System.Windows.Media;
using System.Windows.Shapes;
using Microsoft.Maps.MapControl;

namespace MyFloridaHouse.libSvBingMapsMultiShape 
{
    public class MapMultiGeometry : MapMultiShapeBase
    {
        private IList<PointCollection> _projectedPoints;

        // Methods
        public MapMultiGeometry()
            : base(new Path())
        {
            Vertices = new List<LocationCollection>();
        }

        public void AddPolygonVertices(IList<LocationCollection> locationCollectionList)
        {
            if (Vertices == null) Vertices = new List<LocationCollection>();
            if (locationCollectionList != null)
            {
                PathGeometry polygons = new PathGeometry();

                foreach (var lc in locationCollectionList) //loop through each ring
                {
                    if (!lc[0].Equals(lc[lc.Count - 1])) //check to see if the polygon ring is closed
                    {
                        lc.Add(lc[0]);
                    }
                    Vertices.Add(lc);
                }
            }

        }

        public void AddPolylineVertices(IList<LocationCollection> locationCollectionList)
        {
            if (Vertices == null) Vertices = new List<LocationCollection>();
            if (locationCollectionList != null)
            {
                PathGeometry polygons = new PathGeometry();

                foreach (var lc in locationCollectionList) //loop through each ring
                {
                    Vertices.Add(lc);
                }
            }
        }

        public override IList<PointCollection> ProjectedPoints
        {
            get
            {
                if (_projectedPoints == null) _projectedPoints = new List<PointCollection>();
                return _projectedPoints;
            }
            set
            {
                _projectedPoints = value;
                if (_projectedPoints != null)
                {
                    PathGeometry polylines = new PathGeometry();

                    foreach (PointCollection pc in _projectedPoints) //loop through each ring
                    {
                        PolyLineSegment poly = new PolyLineSegment();
                        poly.Points = pc;

                        PathFigure fig = new PathFigure();
                        fig.StartPoint = pc[0];
                        fig.Segments.Add(poly);

                        polylines.Figures.Add(fig);
                    }

                    //update Path
                    ((Path)base.EncapsulatedShape).Data = polylines;
                }
            }
        }

    }
}
